package logic

import (
	"context"
	"house_srv/models/mysql"

	"house_srv/house_srv"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListHouseLogic {
	return &ListHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListHouseLogic) ListHouse(in *house_srv.ListHouseRequest) (*house_srv.ListHouseResponse, error) {
	// todo: add your logic here and delete this line
	var house mysql.House
	list, err := house.List(in.KeyWord, int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, err
	}

	var sli []*house_srv.House
	for _, h := range list {
		one := house_srv.House{
			Name:    h.Name,
			Price:   float32(h.Price),
			Address: h.Address,
			Hav:     h.Hav,
			Img:     h.Img,
		}
		sli = append(sli, &one)
	}

	return &house_srv.ListHouseResponse{House: sli}, nil
}
